﻿function onConditionDropped(e) {
    var $target = $(e.dropTarget);
    if ($target.is('ul.ws-conditions')) {
        e.preventDefault();
        var nodetext = getTreeviewSelectedItemText($('#conditions'),e);
        var $win = $('#Window').data('tWindow');
        var $newli = $target.addListItem(null);  
        var controltype = $(e.item).attr('conditiontype')
        initialConditionLi($newli, controltype, nodetext);
     
    }
}

function initialConditionLi($newli, conditiontype, nodetext) {
    var $target = $('.ws-conditions');
    var width = $target.parent().width() * 0.95 * 0.25;
    $newli.css('width', width).attr('conditiontype', conditiontype).val(nodetext).find('.display-text').text(nodetext);
    $newli.addHoverFunctions({
        deleteEvent: function () {
            $newli.remove();
        },
        detailEvent: function () {
            setDetailToWindowEditor($newli, initialConditionDetail($newli), $('#Window'));
        }
    });
}

function initialConditionDetail($target) {
    var options = "";
    $('.ws-fields').find('li').each(function () {
        if(options == "")
            options = $(this).data('lidata').FieldName;
        else
            options += "||" + $(this).data('lidata').FieldName;
    });
    var operators = "in||not in||between||>||<||=";
    var data = $target.data('lidata');
    var array = new Array();
        array.push({ controltype: 'text', fieldname: '条件名称', fieldvalue: data==null?null:data.ConditionName, must: true });
        array.push({ controltype: 'dropdownlist', fieldname: '表单选项', fieldvalue: data == null ? null : data.Field, optionalvalue: options, must: true });
        array.push({ controltype: 'dropdownlist', fieldname: '操作符号', fieldvalue: data==null ? null:data.ConditionOperator, optionalvalue: operators, must: true });
        array.push({ controltype: 'text', fieldname: '表单的值', fieldvalue: data==null?null:data.ConditionValue, must: true });
        $target.data('av', array);
    var win = $('#Window').data('tWindow');
    var $table = $target.getTable(
                {
                    save: function () {
                        if ($target.attr('sucess') == 'true') {
                            $target.find('.display-text').text(array[0].fieldvalue + " : " + array[1].fieldvalue + " " + array[2].fieldvalue + " " + array[3].fieldvalue);
                            $target.removeClass('unsave').addClass('saved');
                             saveConditionData($target);
                            win.close();
                        }
                    },
                    cancel: function () {
                        win.close();
                    }
                });
    //绑定默认值
    if (array[2].fieldvalue == null) {
        var defaultvalue;
        var $valuetd;
        $table.find('p:contains("表单选项")').parent().next().find('select').change(function () {
            var $select = $(this);
            var currentfieldname = $select.find(":selected").text()

            $('.ws-fields').find('li').each(function () {
                var data = $(this).data('lidata');
                if (data.FieldName == currentfieldname) {
                    $valuetd = $table.find('p:contains("表单的值")').parent().next();
                    defaultvalue = data.DefaultValue;
                    return;
                }
            });
            if ($valuetd) {
                $valuetd.find(':text').val(defaultvalue);
                defaultvalue = null;
            }
        });
    }
    return $table;
}

function saveConditionData($li) {
    var av = $li.data('av');
    var conditiontype = $li.attr('conditiontype');
    var lidata = { ConditionType: conditiontype, ConditionName: av[0].fieldvalue, Field: av[1].fieldvalue, ConditionOperator: av[2].fieldvalue, ConditionValue: av[3].fieldvalue }
    $li.data('lidata', lidata);
}

function getCondtionsData() {
    var array = new Array();
    $('ul.ws-conditions').children('li').each(function () { 
        var data = $(this).data('lidata');
        var conditiontype = $(this).attr('conditiontype');
        array.push({ ProcessID:$("#processid").val(),ID:$(this).attr('id'), ConditionType: conditiontype, ConditionName: data.ConditionName, Field: { FieldName:data.Field}, ConditionOperator: data.ConditionOperator, ConditionValue: data.ConditionValue });
    });
    return array;
}

function setConditionData(data) {
    if (data) {
        var $ul = $('.ws-conditions');
        for (var i = 0; i < data.length; i++) {
            var $li = $ul.addListItem({ lidata: data[i] });
            controltype = data[i].ConditionType;
            displaytext = data[i].ConditionName;
            initialConditionLi($li, controltype, displaytext);
            $li.attr('id', data[i].id);
        }
    }
}